/*******************************************************************************************
	pachead.cpp					PACHEAD CLASS


********************************************************************************************/

#include "main.h"

/***********************************************************************
Draws pachead on the screen at current position
***********************************************************************/
int Pachead::drawpachead()
{
	GLfloat LightAmbient[]= { 0.0f, 0.0f, 0.0f, 1.0f };
	GLfloat LightDiffuse[]= { 0.0f, 1.0f, 0.0f, 1.0f };
	GLfloat LightPosition[]= { x, y, 10.0f, 1.0f };//

	//LightPosition[0] = x;
	//LightPosition[1] = y;

	///////////////////////////////
	//Determine which way to face
	//////////////////////////////
	glBindTexture(GL_TEXTURE_2D, sprites[DOWN][FRAME]);
	
	////////////////////////////////////
	//determines what color for pachead
	////////////////////////////////////
	switch(state)
	{
	case NORM: 
		{
			if(difficulty > 1)
			{
				LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 1.0f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

				glColor3f(WHITE);
			}
			else
			{
				LightDiffuse[0] = 0.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 0.1f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

				glColor3f(PACGREEN);
			}
		}break;
	case POWER:
		{
			LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 0.0f;
				LightDiffuse[2] = 0.1f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

			glColor3f(PACRED);
			break;
		}
	case SPEED:
		{
			LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 0.1f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);
			glColor3f(PACYELLOW);
			break;
		}
	case INVIS:
		{
			LightDiffuse[0] = 0.5f;
				LightDiffuse[1] = 0.5f;
				LightDiffuse[2] = 0.5f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

			glColor3f(PACGRAY);
			break;
		}
	case GHOST:
		{
			LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 1.0f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);
			glColor3f(WHITE);
			break;
		}
	};

	//if pachead is moving and invisible, make him flash
	if(FLASH == 0 && state == INVIS)
	{
		
		glColor3f(0.05f,0.05f,0.05f);
	}

	for(int loop = 0; loop < numenemy;loop++)
	{
		if(enemies[loop]->hit == true)
		{
			LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 0.5f;
				LightDiffuse[2] = 1.0f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

			glColor3f(1.0f,0.5f,0.0f);
		}
	}


	//if less then 2 seconds left in powerup make him flash
	if(state != NORM && (time-(timeGetTime()-powertimer)) <= 2000)
	{
		if(((time-(timeGetTime()-powertimer))/100)%2 == 0)//flash every 1/10 of a second
		{
			switch(state)
	{
	case NORM: 
		{
			if(difficulty > 1)
			{
				LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 1.0f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

				glColor3f(WHITE);
			}
			else
			{
				LightDiffuse[0] = 0.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 0.1f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

				glColor3f(PACGREEN);
			}
		}break;
	case POWER:
		{
			LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 0.0f;
				LightDiffuse[2] = 0.1f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

			glColor3f(PACRED);
			break;
		}
	case SPEED:
		{
			LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 0.0f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);
			glColor3f(PACYELLOW);
			break;
		}
	case INVIS:
		{
			LightDiffuse[0] = 0.5f;
				LightDiffuse[1] = 0.5f;
				LightDiffuse[2] = 0.5f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

			glColor3f(PACGRAY);
			break;
		}
	case GHOST:
		{
			LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 1.0f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);
			glColor3f(WHITE);
			break;
		}
	};

		}
		else
		{

			if(difficulty > 1)
			{
				LightDiffuse[0] = 1.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 1.0f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

				glColor3f(WHITE);
			}
			else
			{
				LightDiffuse[0] = 0.0f;
				LightDiffuse[1] = 1.0f;
				LightDiffuse[2] = 0.1f;

				glLightfv(GL_LIGHT7, GL_AMBIENT, LightAmbient);	
				glLightfv(GL_LIGHT7, GL_DIFFUSE, LightDiffuse);
				glLightfv(GL_LIGHT7, GL_POSITION,LightPosition);
				glEnable(GL_LIGHT7);

				glColor3f(PACGREEN);
			}
		}
	}
	
	GLUquadric * sphere = gluNewQuadric();
	gluQuadricTexture(sphere, true);

	glPushMatrix();
	glTranslated(x + B_SIZE/2, y + B_SIZE/2,  5.0f);
	
	switch(direction){
		case LEFT:
			{
				glRotated(-90,0,0,1);
				break;
			}
		case DOWN:
			{
				//glRotated(0,0,0,1);
				break;
			}
		case RIGHT:
			{
				glRotated(90,0,0,1);
				break;
			}
		case UP:
			{
				glRotated(180,0,0,1);
				break;
			}
	};

	//glRotated();
	gluSphere(sphere, B_SIZE/3, 20, 20);
	glPopMatrix();
			
	gluDeleteQuadric(sphere);
	/*
	glBegin(GL_QUADS); //draw the tiles
		glTexCoord2f(0.0f, 0.0f); glVertex3f((GLfloat)x, (GLfloat)y,  5.0f);	// Bottom Left Of The Texture and Quad
		glTexCoord2f(1.0f, 0.0f); glVertex3f( (GLfloat)x+B_SIZE, (GLfloat)y,  5.0f);	// Bottom Right Of The Texture and Quad
		glTexCoord2f(1.0f, 1.0f); glVertex3f( (GLfloat)x+B_SIZE,  (GLfloat)y+B_SIZE,  5.0f);	// Top Right Of The Texture and Quad
		glTexCoord2f(0.0f, 1.0f); glVertex3f((GLfloat)x,  (GLfloat)y+B_SIZE,  5.0f);	// Top Left Of The Texture and Quad
	glEnd();
	*/
	glColor3f(WHITE);

	return 0;
}

/***********************************************************************
Gets input from user and moves pachead, and reacts to what he steps on
***********************************************************************/
void Pachead::input()
{
	moving = true; //boolean for moving, default at true

	/////////////////////////
	//check for intersection
	////////////////////////
	/*
	static bool directions[4];
	directions[0] = false;
	directions[1] = false;
	directions[2] = false;
	directions[3] = false;
	if( (int)x%B_SIZE == 0 && (int)y%B_SIZE == 0 )
	{
		if( 
			(direction == RIGHT || direction == LEFT) && 
			((map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL) ||
			 (map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL))   
			||
			(direction == UP || direction == DOWN) && 
			((map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL) ||
			(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL))
		  )
		{   ////////////////////////////////
			//note the available directions
			////////////////////////////////
			if(map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL)
				directions[RIGHT] = true;
			if(map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL)
				directions[LEFT] = true;
			if(map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL)
				directions[UP] = true;
			if(map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL)
				directions[DOWN] = true;

			do//randomly pick from remaining directions
			{
				direction = (rand()%4);
			}while(!directions[direction]);
		}


		if(direction == RIGHT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && map.tiles[posy][posx-1] != WALL && 
		   map.tiles[posy][posx-1] != SWALL)
			direction = LEFT;

		if(direction == LEFT && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && map.tiles[posy][posx+1] != WALL && 
		   map.tiles[posy][posx+1] != SWALL)
			direction = RIGHT;

		if(direction == UP && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy-1][posx] == WALL || 
		   map.tiles[posy-1][posx] == SWALL) && map.tiles[posy+1][posx] != WALL && 
		   map.tiles[posy+1][posx] != SWALL)
			direction = DOWN;

		if(direction == DOWN && (map.tiles[posy][posx-1] == WALL || 
		   map.tiles[posy][posx-1] == SWALL) && (map.tiles[posy][posx+1] == WALL || 
		   map.tiles[posy][posx+1] == SWALL) && (map.tiles[posy+1][posx] == WALL || 
		   map.tiles[posy+1][posx] == SWALL) && map.tiles[posy-1][posx] != WALL && 
		   map.tiles[posy-1][posx] != SWALL)
			direction = UP;
				
	}*////////////////////////////////////////////////////////////////////////////////////////


	/////////////////
	//check for input
	//////////////////
	if(firstPerson){
		switch(direction){
			case LEFT:
				if(keys[VK_RIGHT]){
					keys[VK_RIGHT] = false;
					keys[VK_UP] = true;
					break;
				}
				if(keys[VK_LEFT]){
					keys[VK_LEFT] = false;
					keys[VK_DOWN] = true;
					break;
				}
				if(keys[VK_DOWN]){
					keys[VK_DOWN] = false;
					keys[VK_RIGHT] = true;
					break;
				}
				if(keys[VK_UP]){
					keys[VK_UP] = false;
					keys[VK_LEFT] = true;
					break;
				}
				break;
			case RIGHT:
				if(keys[VK_RIGHT]){
					keys[VK_RIGHT] = false;
					keys[VK_DOWN] = true;
					break;
				}
				if(keys[VK_LEFT]){
					keys[VK_LEFT] = false;
					keys[VK_UP] = true;
					break;
				}
				if(keys[VK_DOWN]){
					keys[VK_DOWN] = false;
					keys[VK_LEFT] = true;
					break;
				}
				if(keys[VK_UP]){
					keys[VK_UP] = false;
					keys[VK_RIGHT] = true;
					break;
				}
				break;
			case DOWN:
				if(keys[VK_RIGHT]){
					keys[VK_RIGHT] = false;
					keys[VK_LEFT] = true;
					break;
				}
				if(keys[VK_LEFT]){
					keys[VK_LEFT] = false;
					keys[VK_RIGHT] = true;
					break;
				}
				if(keys[VK_DOWN]){
					keys[VK_DOWN] = false;
					keys[VK_UP] = true;
					break;
				}
				if(keys[VK_UP]){
					keys[VK_UP] = false;
					keys[VK_DOWN] = true;
					break;
				}
				break;
			case UP:
				break;
		};
	}
	if(keys[VK_RIGHT] &&  direction == LEFT && map.tiles[posy][posx+1] != WALL && map.tiles[posy][posx+1] != SWALL){
		if(firstPerson)
			keys[VK_RIGHT] = false;
		direction = RIGHT;
	}
	if(keys[VK_LEFT] &&  direction == RIGHT && map.tiles[posy][posx-1] != WALL && map.tiles[posy][posx-1] != SWALL){
		if(firstPerson)
			keys[VK_LEFT] = false;
		direction = LEFT;
	}
	if(keys[VK_DOWN] &&  direction == UP && map.tiles[posy+1][posx] != WALL && map.tiles[posy+1][posx] != SWALL){
		if(firstPerson)
			keys[VK_DOWN] = false;
		direction = DOWN;
	}
	if(keys[VK_UP] &&  direction == DOWN && map.tiles[posy-1][posx] != WALL && map.tiles[posy-1][posx] != SWALL) {
		if(firstPerson)
			keys[VK_UP] = false;
		direction = UP;
	}
	

	if(keys[VK_RIGHT] && (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0){
		if(firstPerson)
			keys[VK_RIGHT] = false;
		direction = RIGHT;
	}
	if(keys[VK_LEFT] &&  (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0){
		if(firstPerson)
			keys[VK_LEFT] = false;
		direction = LEFT;
	}
	if(keys[VK_DOWN]&&  (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0) {
		if(firstPerson)
			keys[VK_DOWN] = false;
		direction = DOWN;
	}
	if(keys[VK_UP] &&  (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0){
		if(firstPerson)
			keys[VK_UP] = false;
		direction = UP;
	}

	//speep of speed power-up
	if(state == SPEED && (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0)
		speed = 4*scale;

	//normal speed
	if(state != SPEED && (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0)
		speed = 2*scale;

	////////////////
	//move pachead
	///////////////
	switch(direction)
	{
	case RIGHT:
		{
			x+=speed;
			if(x > 504*scale-((float)W_WIDTH/2))//this code wraps pachead around the screen
			{
				if(state != SPEED)
					x = 2*scale - ((float)W_WIDTH/2);
				else
					x = 4*scale - ((float)W_WIDTH/2);
			}
		}break;
	case LEFT: 
		{
			x-=speed;
			if(x < 0-(W_WIDTH/2))
			{
				if(state != SPEED)
					x = 502*scale-((float)W_WIDTH/2);
				else
					x = 500*scale-((float)W_WIDTH/2);
			}

			
		}break;
	case DOWN:
		{
			y-=speed;
			if( y < (W_HEIGHT-(SIZEY*B_SIZE))-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-B_SIZE-((float)W_HEIGHT/2);
		}break;
	case UP: 
		{
			y+=speed;
			if( y > W_HEIGHT-B_SIZE-((float)W_HEIGHT/2))
				y = (GLfloat)W_HEIGHT-(SIZEY*B_SIZE)-((float)W_HEIGHT/2);

		}break;
	};

	
	//////////////////////
	//hold back if a wall
	//////////////////////
	if(state != GHOST && direction == RIGHT && (map.tiles[posy][posx+1] == WALL || map.tiles[posy][posx+1] == SWALL))  
	{
		x-=speed;
		moving = false;
	}
	if(state != GHOST && direction == LEFT && (map.tiles[posy][posx-1] == WALL || map.tiles[posy][posx-1] == SWALL))
	{
		x+=speed;
		moving = false;
	}
	if(state != GHOST && direction == DOWN && (map.tiles[posy+1][posx] == WALL || map.tiles[posy+1][posx] == SWALL))
	{	y+=speed;
		moving = false;
	}
	if(state != GHOST && direction == UP && (map.tiles[posy-1][posx] == WALL || map.tiles[posy-1][posx] == SWALL))
	{
		y-=speed;
		moving = false;
	}

	/////////////////////////
	//update tile position
	/////////////////////////
	if( (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0 )
	{
		posx = (int)(x+((float)W_WIDTH/2))/B_SIZE;
		posy = 19-(int)(y+((float)W_HEIGHT/2))/B_SIZE;
	}
	
	//////////////////
	//if a pac remove
	//////////////////
	if(map.tiles[posy][posx] == PAC)//if a pac
	{
		map.tiles[posy][posx] = FLOOR;
		map.pacnum--;
		score++;
		soundsystem.PlaySound(sounds[EATPAC]);
	}


	/////////////////////////
	//check for warps
	/////////////////////////
	if(map.tiles[posy][posx] != WARP)
		warp = false;
	if(map.tiles[posy][posx] == WARP  && warp == false)
	{
		
		soundsystem.PlaySound(sounds[WARPSOUND]);

		warp = true;
		
		for(int loop = 0; loop < SIZEY; loop++) 
		{
			for(int loop2 = 0; loop2 < SIZEX; loop2++)
			{
				if(map.special[posy][posx] == map.special[loop][loop2] && (loop != posy || loop2 != posx) && map.special[loop][loop2] != '0')
				{
					posy = loop;
					posx = loop2;
					x = (GLfloat)map.x[loop][loop2];
					y = (GLfloat)map.y[loop][loop2];

					loop = SIZEY;
					loop2 = SIZEX;
				}
			}
		}
	}

	///////////////////
	//check for switch
	///////////////////
	if(map.tiles[posy][posx] != SWITCH)
		swtch = false;
	if(map.tiles[posy][posx] == SWITCH && swtch == false)
	{
		soundsystem.PlaySound(sounds[SWITCHSOUND]);
		swtch = true;
		for(int loop = 0; loop < SIZEY; loop++) 
		{
			for(int loop2 = 0; loop2 < SIZEX; loop2++)
			{
				if(map.special[posy][posx] == map.special[loop][loop2] && (loop != posy || loop2 != posx))
				{
					if(map.tiles[loop][loop2] != SWITCH) //switch walls up or down
					{
						if(map.tiles[loop][loop2] == SWALL)
							map.tiles[loop][loop2] = SWALLD;
						else
						{
							if(map.tiles[loop][loop2] == SWALLD)
								map.tiles[loop][loop2] = SWALL;
						}
					}
				}
			}
		}
	}

	/////////////////////////////
	//check for powerup
	/////////////////////////////
	switch(map.tiles[posy][posx])
	{
	case POWER: 
		{
			state = POWER;
			map.tiles[posy][posx] = FLOOR;
			powertimer = timeGetTime();
			time = 6000;
			soundsystem.PlaySound(sounds[EATPOWER]);
			count1 = false;
			count2 = false;
		}break;
	case SPEED: 
		{
			state = SPEED;
			map.tiles[posy][posx] = FLOOR;
			powertimer = timeGetTime();
			time = 6000;
			soundsystem.PlaySound(sounds[EATSPEED]);
			count1 = false;
			count2 = false;
		}break;
	case INVIS:
		{
			state = INVIS;
			map.tiles[posy][posx] = FLOOR;
			powertimer = timeGetTime();
			time = 13000;
			soundsystem.PlaySound(sounds[EATINVIS]);
			count1 = false;
			count2 = false;
		}break;
	case GHOST: 
		{
			state = GHOST;
			map.tiles[posy][posx] = FLOOR;
			powertimer = timeGetTime();
			time = 3000;
			soundsystem.PlaySound(sounds[EATGHOST]);
			count1 = false;
			count2 = false;
		}break;
	};


	//////////////////////////
	//code for countdown sound
	//////////////////////////
	if(time - (timeGetTime()-powertimer) <= 2000 && count1 == false)
	{
		soundsystem.PlaySound(sounds[COUNTDOWN]);
		count1 = true;
	}


	if(time - (timeGetTime()-powertimer) <= 1000  && count2 == false)
	{
		soundsystem.PlaySound(sounds[COUNTDOWN]);
		count2 = true;
	}

	///////////////////////////
	//if times up, stop powerup
	///////////////////////////
	if(state != NORM)
	{
		if((timeGetTime()-powertimer) >= time && (int)(x+((float)W_WIDTH/2))%B_SIZE == 0 && (int)(y+((float)W_HEIGHT/2))%B_SIZE == 0)
		{
			state = NORM;
			soundsystem.PlaySound(sounds[ENDPOWER]);
			count1 = false;
			count2 = false;
		}
	
	}
	
	//update high score
	if(score >= highscore)
		highscore = score;

}

/***********************************************************************
Death scene for pachead
***********************************************************************/
void Pachead::death()
{
	DWORD timer = 0;
	
	if(difficulty == 1)
		soundsystem.PlaySound(sounds[PACHEADDEATH]);
	else
	{
		int num = 0;

		num = rand()%5;
		soundsystem.PlaySound(dygersound[num]);
	}



	for(int loop = 0; loop < 3; loop++) //loop for 3 frames
	{
		timer = timeGetTime();

		///////////////////////////
		//draw pachead death sceen
		///////////////////////////
		/*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	
		glDisable(GL_DEPTH_TEST);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0,W_WIDTH,0,W_HEIGHT,-1.0,1.0);
		glMatrixMode(GL_MODELVIEW);*/
		
		if(loop != 2)
		{
			glBindTexture(GL_TEXTURE_2D, sprites[4][loop]);

			glBegin(GL_QUADS); //draw the tiles
				glTexCoord2f(0.0f, 0.0f); glVertex3f((GLfloat)x, (GLfloat)y,  0.0f);	// Bottom Left Of The Texture and Quad
				glTexCoord2f(1.0f, 0.0f); glVertex3f( (GLfloat)x+B_SIZE, (GLfloat)y,  0.0f);	// Bottom Right Of The Texture and Quad
				glTexCoord2f(1.0f, 1.0f); glVertex3f( (GLfloat)x+B_SIZE,  (GLfloat)y+B_SIZE,  0.0f);	// Top Right Of The Texture and Quad
				glTexCoord2f(0.0f, 1.0f); glVertex3f((GLfloat)x,  (GLfloat)y+B_SIZE,  0.0f);	// Top Left Of The Texture and Quad
			glEnd();
		}

		SwapBuffers(hDC);

		while((timeGetTime()-timer) < 300);//frame every 1/3 a second
		

	}
	
	///////////////////////
	//reinitialize pachead
	///////////////////////
	posy = initposy;
	posx = initposx;
	x = (GLfloat)map.x[initposy][initposx];
	y = (GLfloat)map.y[initposy][initposx];
	direction = RIGHT;
	state = NORM;
	speed = 2;
	lives--;
	count1 = false;
	count2 = false;
	time = 0;
	health = 100;


	///////////////////////
	//reinitialize enemies
	///////////////////////
	for(int index = 0; index < numenemy; index++)
	{
		enemies[index]->posx = enemies[index]->initposx;
		enemies[index]->posy = enemies[index]->initposy;
		enemies[index]->y = (GLfloat)map.y[enemies[index]->initposy][enemies[index]->initposx];
		enemies[index]->x = (GLfloat)map.x[enemies[index]->initposy][enemies[index]->initposx];
		enemies[index]->direction = RIGHT;
		speed = 1;
	}

}

/***********************************************************************
Loads the sprites and sounds for pachead
***********************************************************************/
void Pachead::loadsprites()
{
	char *spritefiles[] = //hard coded files
	{ 
		"sprite/pachead.bmp",
		"sprite/pachead2.bmp",
		"sprite/pachead3.bmp",
		"sprite/pachead4.bmp",
		"sprite/pachead5.bmp",
		"sprite/pachead6.bmp",
		"sprite/pachead7.bmp",
		"sprite/pachead8.bmp",
		"sprite/pacheadd.bmp",
		"sprite/pacheadd2.bmp",
		"sprite/pacheaddark.bmp",
		"sprite/pacheaddark2.bmp"
	};		
	
	//load each one
	int count = 0;
	for(int index = 0; index < 6; index++)
	{
		for(int loop = 0; loop < 2; loop++)
		{
			sprites[index][loop] = LoadGLTextures(spritefiles[count]);
			count++;
		}
	}


	//sound files
	char *soundfiles[] =
	{
		"sound/eatpac.wav",
		"sound/power.wav",
		"sound/pacheaddeath.wav",
		"sound/warp.wav",
		"sound/invisible.wav",
		"sound/speed.wav",
		"sound/ghost.wav",
		"sound/switch.wav",
		"sound/countdown.wav",
		"sound/endpower.wav",
		"sound/pacheadhit.wav"
	};

	int num;
	for(num = 0; num < 11; num++)
	{
		sounds[num] = soundsystem.LoadSound(soundfiles[num]);
	}

	char *dygersfiles[] =
	{
		"sound/dblah.wav",
		"sound/dblimey.wav",
		"sound/dkilling.wav",
		"sound/dwatch.wav",
		"sound/dlad.wav",
		"sound/douch.wav"
	};


	for(num = 0; num < 6; num++)
	{
		dygersound[num] = soundsystem.LoadSound(dygersfiles[num]);
	}
	
}

void Pachead::loaddyger()
{
	/*
	char *spritefiles[] = //hard coded files
	{ 
		"sprite/dyger.bmp",
		"sprite/dyger2.bmp",
		"sprite/dyger3.bmp",
		"sprite/dyger4.bmp",
		"sprite/dyger5.bmp",
		"sprite/dyger6.bmp",
		"sprite/dyger7.bmp",
		"sprite/dyger8.bmp",
		"sprite/dygerd.bmp",
		"sprite/dygerd2.bmp"
	};
	*/
	
	char *spritefiles[] = //hard coded files
	{ 
		"sprite/egbert.bmp",
		"sprite/egbert1.bmp",
		"sprite/egbert.bmp",
		"sprite/egbert1.bmp",
		"sprite/egbert.bmp",
		"sprite/egbert1.bmp",
		"sprite/egbert.bmp",
		"sprite/egbert1.bmp",
		"sprite/dygerd.bmp",
		"sprite/dygerd2.bmp",
		"sprite/egbert3.bmp",
		"sprite/egbert4.bmp"
	};

	//load each one
	int count = 0;
	for(int index = 0; index < 6; index++)
	{
		for(int loop = 0; loop < 2; loop++)
		{
			sprites[index][loop] = LoadGLTextures(spritefiles[count]);
			count++;
		}
	}
}